package com.oracle.controller;

import com.oracle.annotation.Action;
import com.oracle.annotation.ActionType;
import com.oracle.config.service.api.LoopImageApi;
import com.oracle.news.service.api.NewsServiceApi;
import com.oracle.pojo.Loopimg;
import com.oracle.pojo.News;
import com.oracle.properties.PropertiesUtils;
import com.oracle.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/loop")
public class LoopImageController {

    @Autowired(required = false)
    private NewsServiceApi newsServiceApi;
    @Autowired(required = false)
    private LoopImageApi loopImageApi;
    @Autowired
    private PropertiesUtils propertiesUtils;

    @Action(ActionType.QUERY)
    @RequestMapping("/list")
    public String loopList(Model model){
        ResultEntity<List<Loopimg>> resultEntity = this.loopImageApi.findLoopImageAll();
        model.addAttribute("loopList",resultEntity.getData());
        return "admin/loop/list";
    }

    @Action(ActionType.PAGE_JUMPS)
    @RequestMapping("/addview")
    public String loopAddView(Model model){
        ResultEntity<List<News>> resultEntity = this.newsServiceApi.getNewsIdsAndTitle();
        model.addAttribute("newsList",resultEntity.getData());
        return "admin/loop/addview";
    }

    @Action(ActionType.ADD)
    @RequestMapping("/add")
    public String loopAdd(Loopimg loopimg,HttpServletRequest request,Model model){
        //如果新添加的是加入轮播的状态
        if (loopimg.getLoop()==1) {
            //判断是否达到轮播上限
            int targetCountLoop = this.loopImageApi.getCountLoop();
            int loopmax = Integer.parseInt(this.propertiesUtils.getValue("loopmax"));
            if (loopmax == targetCountLoop) {
                model.addAttribute("msg","已达到轮播上限。。。");
                return "redirect:addview";
            }
        }
        ResultEntity resultEntity = this.loopImageApi.addLoopImage(loopimg);
        return "admin/comm/success";
    }

    @Action(ActionType.UPDATE)
    @RequestMapping("/edit")
    @ResponseBody
    public Map loopEditView(Integer loopImageId, Integer tag, Model model, HttpServletRequest request){
        Map<String,Object> maps = new HashMap<String,Object>();
        //如果是要修改为加入轮播，判断轮播的数量是否已经达到最大
        if (tag==1){
            int targetCountLoop = this.loopImageApi.getCountLoop();
            int loopmax = Integer.parseInt(this.propertiesUtils.getValue("loopmax"));
            if(loopmax==targetCountLoop){
                maps.put("code",201);
                maps.put("msg","轮播数量已达到上限。。");
                return maps;
            }
        }
        //没有达到上限就进行轮播修改
        ResultEntity resultEntity = this.loopImageApi.modifyLoop(tag, loopImageId);
        maps.put("code",resultEntity.getCode());
        maps.put("msg",resultEntity.getMsg());
        return maps;
    }



}
